// pages/mainPage/mainPage.js

const app = getApp()
const networkUtil = app.globalData.networkUtil
const util = app.globalData.util

let logStrPrefix = "mainPage"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        user: null,
        functionModels: null,

        //消息中心
        showMsgNumNotRead: false,
        msgNumNotReadText: "",

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this
        that.setData({
            user: app.globalData.wxUser,
            functionModels: app.globalData.functionModels
        })
        //util.prettyPrint("mainPage.data", that.data)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        let that = this
        /**
         * 1、清空本地文件目录(./reservation/)
         */
        wx.getFileSystemManager().rmdir({
            dirPath: wx.env.USER_DATA_PATH + "/reservation/",
            recursive: true,
            success: function(res) {
                console.info(logStrPrefix + " 清空./reservation/目录中的文件,已完成..")
            }
        })

        /**
         * 2、从缓存中获取 "员工复工二维码",并存入app.globalData.wxUser。
         */
        that.getQrcode_returnToWork()

    },

    /**
     * @Deprecated 已舍弃
     * 从缓存中获取idcardnoQrcodeStr并存入app.globalData.wxUser，若缓存中没有该信息，请求后台获取。
     */
    getIdcardnoQrcode: function() {
        let that = this
        //获取缓存中的idcardnoQrcodeStr
        let idcardnoQrcodeStr = wx.getStorageSync('idcardnoQrcodeStr');

        //若idcardnoQrcodeStr不含有信息
        if (util.isEmpty(idcardnoQrcodeStr)) {
            //复用"/reservation/getReservnoQrcode"后台方法
            let url = app.globalData.baseUrl + "/reservation/getReservnoQrcode"
            let requestData = {
                reservno: app.globalData.wxUser.idcardno
            }

            networkUtil.request_get(url, requestData, false).then((res) => {
                console.info(logStrPrefix + ".getIdcardnoQrcode() 从后台获取身份证二维码,已完成..")

                //若已获取到idcardno对应的二维码字符串
                app.globalData.wxUser.idcardnoQrcodeStr = res.data.reservnoQrcodeStr
                //将idcardnoQrcodeStr存入缓存
                wx.setStorageSync('idcardnoQrcodeStr', res.data.reservnoQrcodeStr);
            }).catch((res) => {
                //若未获取到idcardno对应的二维码字符串
                console.error(logStrPrefix + ".getIdcardnoQrcode() 获取idcardno对应的二维码字符串,出错!")
            })
        }

        //若idcardnoQrcodeStr含有信息
        if (!util.isEmpty(idcardnoQrcodeStr)) {
            //将idcardnoQrcodeStr存入app.globalData.wxUser
            app.globalData.wxUser.idcardnoQrcodeStr = idcardnoQrcodeStr
        }
    },

    /**
     * 从缓存中获取 "员工复工二维码",并存入app.globalData.wxUser，
     * 若缓存中没有该信息，请求后台获取。
     */
    getQrcode_returnToWork: function() {
        let that = this
        //从缓存中获取 "员工复工二维码"
        let qrcodeStr_canReturnToWork = wx.getStorageSync("qrcodeStr_canReturnToWork")
        let qrcodeStr_cannotReturnToWork = wx.getStorageSync("qrcodeStr_cannotReturnToWork")

        let isExist = !util.isEmpty(qrcodeStr_canReturnToWork) && !util.isEmpty(qrcodeStr_cannotReturnToWork)

        //若缓存中没有 "员工复工二维码"
        if (isExist == false) {
            let url = app.globalData.baseUrl + "/employeeInoutRecord/getEmplQrcodeStr_returnToWork"
            let requestData = {

            }
            networkUtil.request_get(url, requestData, false).then((res) => {
                console.info(logStrPrefix + ".getQrcode_returnToWork() 从后台获取复工二维码,已完成..")

                //若已获取到
                app.globalData.wxUser.qrcodeStr_canReturnToWork = res.data.emplQrcodeStr_canReturnToWork
                app.globalData.wxUser.qrcodeStr_cannotReturnToWork = res.data.emplQrcodeStr_cannotReturnToWork
                //存入缓存
                wx.setStorageSync("qrcodeStr_canReturnToWork", res.data.emplQrcodeStr_canReturnToWork)
                wx.setStorageSync("qrcodeStr_cannotReturnToWork", res.data.emplQrcodeStr_cannotReturnToWork)
            }).catch((res) => {
                //若未获取到
                console.error(logStrPrefix + ".getQrcode_returnToWork() 从后台获取复工二维码,出错!", res)
            })
        }

        //若缓存中有 "员工复工二维码"
        if (isExist == true) {
            //将 "复工二维码"存入app.globalData.wxUser中
            app.globalData.wxUser.qrcodeStr_canReturnToWork = qrcodeStr_canReturnToWork
            app.globalData.wxUser.qrcodeStr_cannotReturnToWork = qrcodeStr_cannotReturnToWork
        }
    },
	
    /**
     * 消息中心》获取未读消息数量。
     */
    getMsgNumNotRead: function() {
        /**
         * 消息中心》获取未读消息数量。
         */
        let that = this
        let url = app.globalData.baseUrl + "/msgcenter/getMcMessageNumNotRead"
        let requestData = {}
        networkUtil.request_get(url, requestData, false).then((res) => {
            //若已获取到
            let msgNumNotRead = res.data.msgNumNotRead
            //若未读消息为0
            if (msgNumNotRead == 0) {
                that.setData({
                    showMsgNumNotRead: false,
                    msgNumNotReadText: '0'
                })
            }
            //若未读消息数量处于(0,99]
            if (msgNumNotRead > 0 && msgNumNotRead < 100) {
                that.setData({
                    showMsgNumNotRead: true,
                    msgNumNotReadText: msgNumNotRead
                })
            }
            if (msgNumNotRead >= 100) {
                that.setData({
                    showMsgNumNotRead: true,
                    msgNumNotReadText: "99+"
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        //console.log(logStrPrefix+" onShow()..")

        let that = this
        /**
         * 1、消息中心》获取未读消息数量。
         */
        that.getMsgNumNotRead()




    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})